package com.tg.app.activity.device.settings;

import static com.tg.app.activity.device.DeviceSettingsActivity.EXT_DEVICE_INFO;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ListView;

import androidx.annotation.RequiresApi;

import com.tg.app.R;
import com.tg.app.activity.base.DeviceSettingsBaseActivity;
import com.tg.app.activity.device.DeviceSettingsActivity;
import com.tg.app.adapter.SmartSpeakerAdapter;
import com.tg.app.util.UriUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.appcommon.android.TGToast;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.data.http.entity.SmartSpeakerBean;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SmartSpeakerActivity extends DeviceSettingsBaseActivity{

    private final ArrayList<SmartSpeakerBean> smartSpeaks = new ArrayList<>();
    private SmartSpeakerAdapter adapter ;

    private DeviceSettingsInfo info;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        hideActionBar();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_speakers_mgr);
        initView();

        Intent intent = getIntent();
        info  = intent.getParcelableExtra(DeviceSettingsActivity.EXT_DEVICE_INFO);
        getSmartSpeakers(info.deviceID);
        backClickEvent();
    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void initView() {
        adapter = new SmartSpeakerAdapter(smartSpeaks,this);
        ListView listView = findViewById(R.id.user_list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            if (smartSpeaks.size() > 0 && position >= 0 && position < smartSpeaks.size()){
                SmartSpeakerBean lockBellUserBean = smartSpeaks.get(position);
                TGLog.d("url:" + lockBellUserBean.getUrl() + ", name:" + lockBellUserBean.getName());
                String url = lockBellUserBean.getUrl();
                Intent intent = SmartSpeakerActivity.this.getIntent();
                UriUtil.openServePlayImpl(SmartSpeakerActivity.this, intent, url, "", 0);
            }
        });
    }



    public void getSmartSpeakers(final long deviceId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("device_id", String.valueOf(deviceId));

        TGHttp.getInstance().smartSpeakers(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<List<SmartSpeakerBean>>() {
                    @Override
                    protected void onSuccess(final List<SmartSpeakerBean> items) {
                        TGLog.d( "content = " +  items.size());
                        smartSpeaks.clear();
                        smartSpeaks.addAll(items);
                        findViewById(R.id.no_user_linearlayout).setVisibility(smartSpeaks.size() == 0? View.VISIBLE : View.GONE);
                        findViewById(R.id.user_list).setVisibility(smartSpeaks.size() != 0? View.VISIBLE : View.GONE);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        TGLog.d("errorInfo = " + errorInfo);
                        TGToast.showToast(errorInfo);
                        finish();
                    }

                    @Override
                    protected void onOtherError(String error) {
                        TGLog.d("error = " + error);
                        TGToast.showToast(error);
                        finish();
                    }
                });
    }



    @Override
    protected boolean onBackClick() {
        Intent intent = new Intent();
        intent.putExtra(EXT_DEVICE_INFO,info);
        setResult(RESULT_OK,intent);
        return super.onBackClick();
    }
}
